package com.tour.tour.controller;

import cn.hutool.core.bean.BeanUtil;
import com.tour.tour.common.Response;
import com.tour.tour.dto.LoginFormDTO;
import com.tour.tour.dto.RegisterFormDTO;
import com.tour.tour.dto.UserDTO;
import com.tour.tour.entity.User;
import com.tour.tour.entity.UserInfo;
import com.tour.tour.service.UserInfoService;
import com.tour.tour.service.UserService;
import com.tour.tour.utils.UserHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 76004
 * @apiNote
 * @date 2023/4/24 22:43
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Resource
    private UserService userService;
    
    @Resource
    private UserInfoService userInfoService;

    /**
     * 发送手机验证码
     * @param phone
     * @param session
     * @return
     */
    @PostMapping("/code")
    public Response sendCode(@RequestParam("phone") String phone, HttpSession session) {
        //System.out.println("phone: " + phone);
        return userService.sendCode(phone, session);
    }

    /**
     * 登录功能
     * @param loginForm
     * @param session
     * @return
     */
    @PostMapping("/login")
    public Response login(@RequestBody LoginFormDTO loginForm, HttpSession session) {
        return userService.login(loginForm, session);
    }
    
    @PostMapping("/register")
    public Response register(@RequestBody RegisterFormDTO registerForm, HttpSession session) {
        return userService.register(registerForm, session);
    }
    
    @PostMapping("/forgot")
    public Response forgot(@RequestBody RegisterFormDTO forgotForm, HttpSession session) {
        return userService.forgotPassword(forgotForm, session);
    }
    
    @PostMapping("/logout")
    public Response logout() {
        // 登出功能
        UserHolder.removeUser();
        return Response.success();
    }

    /**
     * 获取当前登录用户
     * @return
     */
    @GetMapping("/me")
    public Response me() {
        // 获取当前登录用户并返回
        UserDTO userDTO = UserHolder.getUser();
        return Response.success(userDTO);
    }
    
    @GetMapping("/info/{id}")
    public Response info(@PathVariable("id") int userId) {
        UserInfo userInfo = userInfoService.getById(userId);
        if (userInfo == null) {
            return Response.success();
        }
        userInfo.setCreateTime(null);
        userInfo.setUpdateTime(null);
        
        return Response.success(userInfo);
    }
    
    @GetMapping("/{id}")
    public Response queryById(@PathVariable("id") int userId) {
        User user = userService.getById(userId);
        if (user == null) {
            return Response.success();
        }
        UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
        return Response.success(userDTO);
    }
    
    
}
